package com.example.myapplication2;

import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class SwitchActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_switch);
        textView = findViewById(R.id.sw_status);
        Switch sw_status = findViewById(R.id.sw1);
        Switch sw_status2 = findViewById(R.id.sw2);
        Switch sw_status4 = findViewById(R.id.sw4);
        //设置该开关的轨迹样式
        sw_status4.setThumbDrawable(getDrawable(R.drawable.ic_launcher_foreground));
        //设置该开关的默认状态
        sw_status2.setChecked(true);
        sw_status.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        switch (compoundButton.getId()){
            case R.id.sw1:
                textView.setText(String.format("Switch状态为%s",b));
                break;
        }
    }
}